package com.smart.controller.sys;

import java.util.Map;
import java.util.List;
import com.smart.entity.SsSite;
import com.smart.util.CommonPage;
import javax.annotation.Resource;
import io.swagger.annotations.Api;
import com.smart.util.CommonResult;
import com.smart.vo.SiteInsertVo;
import com.smart.service.SsSiteService;
import com.smart.common.annotation.SysLog;
import io.swagger.annotations.ApiOperation;
import com.smart.common.enums.BusinessType;
import com.smart.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020年6月22日 下午5:17:41
 * Copyright (C) 深圳工勘科技
 * @说明: 工地管理
 **/
@RestController("sysSiteController")
@RequestMapping("/sys/site/")
@Api(tags="Sys-site => 工地管理")
public class SysSiteController extends BaseController{

	@Resource
	private SsSiteService siteService;
	
	@ApiOperation("page => 根据名获取工地分页")
    @GetMapping(value="page")
	@SysLog(title="工地管理",businessType=BusinessType.FIND)
    public CommonResult<CommonPage<SsSite>> list(
          @RequestParam(value="pageSize") Integer pageSize,
          @RequestParam(value="pageNum") Integer pageNum) {
        IPage<SsSite> lists = siteService.findPage(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(lists));
    }
	
	@ApiOperation("{id} => 根据工地编号获取详细信息")
    @GetMapping(value="/{id}")
    public CommonResult<SsSite> getInfo(@PathVariable Integer id) {
        return CommonResult.success(siteService.findById(id));
    }
	
	@ApiOperation("update/{id} => 修改指定工地")
    @SysLog(title="工地管理",businessType=BusinessType.UPDATE)
    @PutMapping(value="/update/{id}")
    public CommonResult<Integer> update(@PathVariable Integer id,
    		@RequestBody SsSite site) {
        int count = siteService.updateSite(id,site);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
	
	@ApiOperation("list => 获取工地列表")
    @GetMapping(value="list")
	@SysLog(title="工地管理",businessType=BusinessType.FIND)
    public CommonResult<List<SsSite>> list() {
        List<SsSite> lists = siteService.findAll();
        return CommonResult.success(lists);
    }
	
	@ApiOperation("select_user=>获取工地用户")
    @GetMapping(value="select_user")
	@SysLog(title="工地管理",businessType=BusinessType.FIND)
    public CommonResult<Map<String,Object>> selectUser(
    	@RequestParam(name="siteId") Integer siteId){
        Map<String,Object> data = siteService.findSysUserOnSite(siteId);
        return CommonResult.success(data);
    }
	
	@ApiOperation("insert_user=>插入工地用户")
    @PostMapping(value="insert_user")
	@SysLog(title="工地管理",businessType=BusinessType.INSERT)
    public CommonResult<Map<String,Object>> selectUser(
    	@RequestBody SiteInsertVo vo){
        siteService.deleteUserBySiteId(vo.getSiteId(),vo.getUserIds());
        return CommonResult.success(null);
    }
	
}